package com.spark.tags;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class Button extends ViewComponent {
	
	public static final String TAG_NAME = "button";
	
	public static final String PROPERTITY_TEXT = "text";
	public static final String PROPERTITY_TOOLTIP = "tooltip";
	public static final String PROPERTITY_TOOLTIP_STYLE = "tooltipStyle";
	public static final String PROPERTITY_TOOLTIP_ALIGN = "tooltipAlign";
	public static final String PROPERTITY_TEXT_CLASS = "textClass";
	public static final String PROPERTITY_ICON_CLASS = "iconClass";
	public static final String PROPERTITY_CLICK = "click";
	
	private static final String DEFAULT_BUTTON_CLASSNAME = "s-btn";	
	private static final String DEFAULT_TEMPLATES = "button.ftl";
	
	private static final int DEFAULT_HEIGHT = 25;
	private static final int DEFAULT_WIDTH = 60;
	

	protected int getDefaultWidth(){
		return DEFAULT_WIDTH;
	}
	
	protected int getDefaultHeight(){
		return DEFAULT_HEIGHT;
	}
	
	public String getTemplate(){
		return DEFAULT_TEMPLATES;
	}
	
	protected String getDefaultClass(XMap view,SparkContext context){
		return DEFAULT_BUTTON_CLASSNAME;
	}
	
	
	public void init(XMap view, SparkContext context) throws Exception {
		String clickHandler = view.getString(PROPERTITY_CLICK, "");
		if(!"".equals(clickHandler)){
			boolean hasClick = false;
			XMap events = view.getChild(PROPERTITY_EVENTS);
			if(events == null){
				events = new XMap(DEFAULT_TAG_PREFIX,DEFAULT_NAME_SPACE,PROPERTITY_EVENTS);
				view.addChild(events);
			}else{
				List<XMap> list = events.getChilds();
				if(list != null){
					Iterator<XMap> it = list.iterator();
					while(it.hasNext()){
						XMap event = it.next();
						String eventName = event.getString(PROPERTITY_EVENT_NAME, "");
						if(PROPERTITY_CLICK.equals(eventName)) {
							hasClick = true;
							break;
						}
					}
				}
			}
			if(!hasClick) {
				XMap event = new XMap(DEFAULT_TAG_PREFIX,DEFAULT_NAME_SPACE,PROPERTITY_EVENT);
				event.putString(PROPERTITY_EVENT_NAME, PROPERTITY_CLICK);
				event.putString(PROPERTITY_EVENT_HANDLER, clickHandler);
				events.addChild(event);
			}
		}
		super.init(view, context);
		
		
		Integer width = (Integer)context.get(PROPERTITY_WIDTH);
		context.put(PROPERTITY_WIDTH, new Integer(width.intValue()-2));
		
		String text = view.getString(PROPERTITY_TEXT, "");
		if("".equals(text)){
			context.put(PROPERTITY_TEXT_CLASS, "e");
			text = "&#160;";
		}
		context.put(PROPERTITY_TEXT, text);
		
		String tooltip = view.getString(PROPERTITY_TOOLTIP,"");
		if(!"".equals(tooltip)){
			String tip = "data-tooltip-type=\"tip\" data-tooltip=\""+tooltip+"\"";
			String tipalgin = view.getString(PROPERTITY_TOOLTIP_ALIGN,"");
			if(!"".equals(tipalgin)){
				tip = tip + " data-tooltip-align=\""+tipalgin+"\"";
			}
			String tipStyle = view.getString(PROPERTITY_TOOLTIP_STYLE,"black");
			if(!"".equals(tipStyle)){
				tip = tip + " data-tooltip-style=\""+tipStyle+"\"";
			}
			
			context.put(PROPERTITY_TOOLTIP, tip);
		}
		
		
		String iconClass = view.getString(PROPERTITY_ICON_CLASS, "e");
		if(!"e".equals(iconClass)){
			iconClass = "icon " + iconClass;
		}		
		context.put(PROPERTITY_ICON_CLASS, iconClass);
	}
	
	@SuppressWarnings("unchecked")
	protected String build(SparkContext context){
		Map map = context.getMap();
		StringBuilder sb = new StringBuilder();
		sb.append("<div ").append(get(map,"tooltip")).append(" class=\"").append(get(map,"className")).append("\" style=\"width:").append(get(map,"width")).append("px;height:").append(get(map,"height")).append("px;line-height:").append(get(map,"height")).append("px;").append(get(map,"style")).append("\" id=\"").append(get(map,"id")).append("\"  tabIndex=\"").append(get(map,"tabIndex")).append("\">");
		sb.append("<span class=\"").append(get(map,"iconClass")).append("\"></span>");
		sb.append("<span class=\"").append(get(map,"textClass")).append("\" stype=\"text\">").append(get(map,"text")).append("</span>");
		sb.append("</div>");
		sb.append("<script>new Spark.Button(").append(get(map,"config")).append(");</script>");
		return sb.toString();
	}
}
